package jfox.platform.logging.action;

import javax.ejb.EJB;

import jfox.platform.common.PaginationList;
import jfox.platform.common.Utils;
import jfox.platform.infrastructure.SuperAction;
import jfox.platform.logging.bo.LogBO;
import jfox.platform.logging.entity.Log;
import org.jfox.framework.annotation.Service;
import org.jfox.mvc.ActionContext;
import org.jfox.mvc.Invocation;
import org.jfox.mvc.PageContext;
import org.jfox.mvc.annotation.ActionMethod;
import org.jfox.mvc.validate.DateValidation;
import org.jfox.mvc.validate.LongDateValidator;

/**
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 */
@Service(id="logging")
public class LogAction extends SuperAction {

    @EJB
    LogBO logBO;

    @ActionMethod(name = "mgmt", successView = "platform/logging/log_management.vhtml", invocationClass = LogInvocation.class)
    public void logManagement(ActionContext actionContext) throws Exception {
        LogInvocation invocation = (LogInvocation)actionContext.getInvocation();
        String username = invocation.getName();
        if(username == null || username.trim().length() == 0) {
            username = getUserContext().getUsername();
        }
        long moduleId = invocation.getModuleId();
        long startDate = invocation.getStartDate(); // 如果没有输入，则会使用 Invocation 的默认值
        long endDate = invocation.getEndDate(); // 如果没有输入，则会使用 Invocation 的默认值
        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("selectedModule", moduleId);
        pageContext.setAttribute("selectedUser", username);
        pageContext.setAttribute("selectedStartDate", Utils.translateDate(startDate));
        pageContext.setAttribute("selectedEndDate", Utils.translateDate(endDate));

    }

    @ActionMethod(name = "search", successView = "include/json_response.vhtml", invocationClass = LogInvocation.class)
    public void searchLog(ActionContext actionContext) throws Exception {
        LogInvocation invocation = (LogInvocation)actionContext.getInvocation();
        String username = invocation.getName();
        if(username == null || username.trim().length() == 0) {
            username = getUserContext().getUsername();
        }
        long moduleId = invocation.getModuleId();
        long startDate = invocation.getStartDate(); // 如果没有输入，则会使用 Invocation 的默认值
        long endDate = invocation.getEndDate(); // 如果没有输入，则会使用 Invocation 的默认值
        int start = invocation.getStart();
        int limit = invocation.getLimit();
        String type = invocation.getType();
        PaginationList<Log> logs = logBO.searchLog(username,moduleId,type,startDate* 1000000, (endDate+1)* 1000000, start, limit);

        PageContext pageContext = actionContext.getPageContext();
        // 转换成 ExtJSPagedList, 因为需要传递 TotalCount
        pageContext.setAttribute(JSON_RESPONSE_OBJ, logs);
    }

    public static class LogInvocation extends Invocation {

        private String name;

        // nullable 设为true以让没有该参数输入时使用默认值
        @DateValidation(validatorClass = LongDateValidator.class, nullable = true)
        private long startDate = Utils.getTodayLong();

        // nullable 设为true以让没有该参数输入时使用默认值
        @DateValidation(validatorClass = LongDateValidator.class, nullable = true)
        private long endDate = Utils.getTodayLong();
        
        private String type;
        private long moduleId;

        // 分页参数
        private int start = 0;
        private int limit = 25;

        public long getEndDate() {
            return endDate;
        }

        public void setEndDate(long endDate) {
            this.endDate = endDate;
        }

        public long getStartDate() {
            return startDate;
        }

        public void setStartDate(long startDate) {
            this.startDate = startDate;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public long getModuleId() {
            return moduleId;
        }

        public void setModuleId(long moduleId) {
            this.moduleId = moduleId;
        }

        public int getStart() {
            return start;
        }

        public void setStart(int start) {
            this.start = start;
        }

        public int getLimit() {
            return limit;
        }

        public void setLimit(int limit) {
            this.limit = limit;
        }
    }

}
